﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using EnvControlLib;
using System.Windows.Media;
using SurveillancePro.Base.Events;
using SurveillanceLib;
using CommonClass;
using CommonBase;
using DataLib;


namespace EnvCommonUtility
{
    /// <summary>
    /// 用于视频界面的分屏，具备关闭与打开视频功能
    /// </summary>
    public class EnvPanelOperation
    {
        #region 属性

        private static EnvVideoPanel _selectedPanel;
        /// <summary>
        /// 记录所选 Panel，只能选择一个 Panel【Static】
        /// </summary>
        public static EnvVideoPanel SelectedPanel
        {
            get { return _selectedPanel; }
            set
            {
                if (_selectedPanel == value) return;
                _selectedPanel = value;

                try
                {
                    if (EnvUtilities.EnvEventAggregator != null)
                    {
                        // 修改云台控制按钮状态
                        System.Windows.Visibility isShowPTZControlButton = System.Windows.Visibility.Collapsed;
                        if (value != null &&
                            value.Tag != null &&
                            (value.Tag is TagModel) &&
                            (value.Tag as TagModel).Driver_Instance != null &&
                            !string.IsNullOrWhiteSpace((value.Tag as TagModel).Driver_LoginID) &&
                            !string.IsNullOrWhiteSpace((value.Tag as TagModel).Panel_SelectedChannel) &&
                            !string.IsNullOrWhiteSpace((value.Tag as TagModel).Panel_RealPlayHandle))
                            isShowPTZControlButton = System.Windows.Visibility.Visible;

                        EnvUtilities.EnvEventAggregator.GetEvent<OpenModelEvent>().Publish(new OpenModelEventArg() { ModelName = "ViewModel_PTZControl", FunctionName = "ChangePTZControlState", Arg = isShowPTZControlButton, }); // 事件通知
                    }
                }
                catch (System.Exception ex)
                {
#if DEBUG
                    Debug.WriteLine(ex.Message);
#endif
                }
            }
        }

        #endregion

        #region 公开的方法

        /// <summary>
        /// 将 SelectedPanel 设为 null
        /// </summary>
        public void SetNull2SelectedPanel()
        {
            SelectedPanel = null;
        }

        /// <summary>
        /// 分屏
        /// </summary>
        /// <param name="screenCount">分屏个数</param>
        /// <param name="splitScreen">容器窗口</param>
        /// <param name="maxScreen">最大化窗口</param>
        /// <param name="currentList">记录大屏已经创建好的 Panel 数组 | 相关列表，主要用于修改新创建的小分屏</param>
        /// <param name="doubleAction">共同的双击事件</param>
        /// <returns></returns>
        public List<EnvVideoPanel> SplitScreen(int screenCount, System.Windows.Controls.Panel splitScreen,
            EnvVideoPanel maxScreen, List<EnvVideoPanel> currentList, Action doubleAction = null)
        {
            try
            {
                int outChannel;
                SplitScreen splitscreen = new SplitScreen(); // 画面分割
                List<EnvVideoPanel> panelCurrentList = new List<EnvVideoPanel>(); // 记录大屏新创建的 Panel 数组
                List<EnvVideoPanel> panelHistoryList = new List<EnvVideoPanel>(); // 记录上一次系统创建的 Panel 数组
                Size size = new Size()
                {
                    Height = (int)splitScreen.ActualHeight,
                    Width = (int)splitScreen.ActualWidth,
                };
                IList<Rectangle> rectList = splitscreen.CalcPanelRectangle(screenCount, size);

                splitScreen.Children.Clear(); // 先清除 Panel 已存在的内容
                // 1. 将当前正在播放的视频停止
                if (currentList != null)
                {
                    foreach (EnvVideoPanel currPanel in currentList)
                    {
                        if (currPanel.Tag != null &&
                            !string.IsNullOrWhiteSpace((currPanel.Tag as TagModel).Panel_RealPlayHandle))
                        {
                            TagModel currPanelTag = currPanel.Tag as TagModel;
                            if (currPanelTag.Driver_Instance.
                                StopRealPlay(currPanelTag.Panel_RealPlayHandle)) // 停止实时监控，需要传递实时监控句柄
                                currPanelTag.Panel_RealPlayHandle = string.Empty;
                            else
                            {
                                MessageBox.Show(SurveillanceUtilities.STOP_REALPLAY_FAILURE, ConstantString.MSG_COMPANY);
                                return null;
                            }
                        }

                        panelHistoryList.Add(currPanel); // 2. 将已停止的视频数组放入历史数组中
                    }
                }

                for (int i = 0; i < rectList.Count; i++)
                {
                    EnvVideoPanel panel = new EnvVideoPanel()
                    {
                        Width = rectList[i].Width,
                        Height = rectList[i].Height,
                        Tag = new TagModel()
                        {
                            Panel_Index = i,
                        },
                    };

                    if (panel.DataContext != null &&
                        panel.DataContext is ViewModel_EnvVideoPanel)
                    {
                        ViewModel_EnvVideoPanel vm_EnvVideoPanel = panel.DataContext as ViewModel_EnvVideoPanel;
                        vm_EnvVideoPanel.IsMaxPanel = false;
                        vm_EnvVideoPanel.IsRealPlayPanel = System.Windows.Visibility.Visible;
                        vm_EnvVideoPanel.PanelCurrentList = panelCurrentList;
                        vm_EnvVideoPanel.SplitScreen = splitScreen;
                        vm_EnvVideoPanel.MaxScreen = maxScreen;
                        vm_EnvVideoPanel.DoubleAction = doubleAction;
                        vm_EnvVideoPanel.ChangeBorderColorAction = EnvUtilities.ChangeBorderColor;
                        vm_EnvVideoPanel.ShowMaxScreenAction = EnvUtilities.ShowMaxScreen;
                        vm_EnvVideoPanel.HideMaxScreenAction = EnvUtilities.HideMaxScreen;
                        vm_EnvVideoPanel.StopPanelRealPlayAction = EnvUtilities.StopPanelRealPlay;
                        vm_EnvVideoPanel.RecordVideoAction = EnvUtilities.RecordVideo;
                        vm_EnvVideoPanel.PlayBackAction = EnvUtilities.PlayBack;
                        vm_EnvVideoPanel.ScreenShotAction = EnvUtilities.ScreenShot;
                    }

                    // 3. 在新分屏数组中播放已保存的历史视频
                    if (panelHistoryList != null && panelHistoryList.Count > 0 &&
                        i < panelHistoryList.Count && panelHistoryList[i] != null &&
                        panelHistoryList[i].Tag != null &&
                        panelHistoryList[i].Tag is TagModel)
                    {
                        TagModel tag = panelHistoryList[i].Tag as TagModel;
                        if ((panelHistoryList[i].Tag as TagModel).Driver_Instance != null &&
                            !string.IsNullOrWhiteSpace((panelHistoryList[i].Tag as TagModel).Driver_LoginID) &&
                            !string.IsNullOrWhiteSpace((panelHistoryList[i].Tag as TagModel).Panel_SelectedChannel) &&
                            int.TryParse((panelHistoryList[i].Tag as TagModel).Panel_SelectedChannel, out outChannel) &&
                            string.IsNullOrWhiteSpace((panelHistoryList[i].Tag as TagModel).Panel_RealPlayHandle)) // 未播放时
                        {
                            string pRealPlayHandle = tag.Driver_Instance.RealPlay(tag.Driver_LoginID,
                                    outChannel, (panel.DataContext as ViewModel_EnvVideoPanel).CurrentPanel.Handle); // 只取数据不播放

                            tag.Panel_RealPlayHandle = pRealPlayHandle; // 得到最新的 RealPlayHandle
                            if (string.IsNullOrWhiteSpace((panelHistoryList[i].DataContext as ViewModel_EnvVideoPanel).VideoInformation)) // 一般视频窗口与最大化视频窗口标题信息不一致时
                            {
                                (panel.DataContext as ViewModel_EnvVideoPanel).VideoInformation =
                                    (panelHistoryList[i].DataContext as ViewModel_EnvVideoPanel).TempInformation; // 设置标题信息
                            }
                            else
                            {
                                (panel.DataContext as ViewModel_EnvVideoPanel).VideoInformation =
                                    (panelHistoryList[i].DataContext as ViewModel_EnvVideoPanel).VideoInformation; // 设置标题信息
                            }
                        }

                        Utilities.CopyObject(tag, panel.Tag);
                    }

                    splitScreen.Children.Add(panel);
                    panelCurrentList.Add(panel); // 5. 加入到当前 Panel 列表
                }

                return panelCurrentList;
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                return null;
            }
        }

        #endregion
    }
}
